﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using MegaSportsStore.Domain.MegaEntities;
using MegaSportsStore.Domain.MegaAbstract;
using Moq;
using MegaSportsStore.Domain.MegaConcrete;
using System.Web.Configuration;

namespace MegaSportsStore.WebUI.MegaInfrastructure_
{
    public class MegaNinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public MegaNinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);

        }

        private void AddBindings()
        {
            var b = WebConfigurationManager.AppSettings["MockMode"];
            if (b.ToUpper().CompareTo("TRUE") == 0)
            {

                // was before using the Moq, now commented coz to use the actual implementation.
                Mock<IMegaProductRepository> mock = new Mock<IMegaProductRepository>();
                mock.Setup(m => m.MegaProducts).Returns(new List<MegaProduct> {
                new MegaProduct {Id = 1, Name="Football", Price = 25},
                new MegaProduct {Id = 2, Name="Surf board", Price = 179},
                new MegaProduct {Id = 3, Name="Running Shoes", Price = 95}
            }.AsQueryable());

                ninjectKernel.Bind<IMegaProductRepository>().ToConstant(mock.Object);
            }
            else
            {

                ninjectKernel.Bind<IMegaProductRepository>().To<MegaEFProductRepository>();
            }
        }
    }
}